package com.framework.modules.base.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.common.annotation.Login;
import com.framework.common.config.SystemConfigName;
import com.framework.common.utils.*;
import com.framework.modules.app.entity.MixedOnlineUserEntity;
import com.framework.modules.app.entity.MixedRewardsToplistEntity;
import com.framework.modules.app.entity.ViewUserOnlineListEntity;
import com.framework.modules.app.service.MixedOnlineUserService;
import com.framework.modules.app.service.MixedRewardsToplistService;
import com.framework.modules.app.service.ViewUserOnlineListService;
import com.framework.modules.base.entity.AdEntity;
import com.framework.modules.base.service.AdService;
import com.framework.modules.base.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 文章表 
 *
 * @author MIT
 * @email litmiss@gmail.com
 * @date 2020-10-27 10:46:10
 */
@RestController
@RequestMapping("/mixed")
@Api(tags = "综合数据")
public class MixedController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private AdService adService;

    @Autowired
    private MixedOnlineUserService mixedOnlineUserService;

    @Autowired
    private MixedRewardsToplistService mixedRewardsToplistService;

    @Autowired
    private ViewUserOnlineListService viewUserOnlineListService;

    /**
     *推荐好友
     * 数据库 [ 用户信息表] tb_user_info
     * @return
     */
    @Login
    @GetMapping("/get_recommand_user_list")
    @ApiOperation("推荐好友")
    public R getRecommandUserList(){
        Long uid = (Long) HttpContextUtils.getHttpServletRequestHeaderValue("uid");

        Object object=userInfoService.getRecommandUserList(QuickHelper.getConfigIntegerValue(SystemConfigName.RECORD_USER_RECOMMAND_COUNT),uid);
        return R.ok(object);
    }

    /**
     *广告展示
     * 数据库 [ 广告表 ] tb_ad
     * @return
     * code 编码
     * type 类型 首页=home
     * link 链接
     * title 标题
     * pic_url 图片
     * expired_date 过期时间 过期则不显示
     */
    @Login
    @GetMapping("/get_ad_item")
    @ApiOperation("广告展示")
    public R getAdItem(String type){
        Object object=adService.selectList(new EntityWrapper<AdEntity>().eq("type",type)
                .gt("expired_date", DateUtils.getSecondTimestamp(new Date())).last("limit 1"));
        return R.ok(object);
    }

    /**
     *获取在线用户列表
     * 数据库 [ 在线用户列表 ] tb_mixed_online_user
     * @return
     * uid 用户编号
     * nickname 用户昵称
     * avanta 用户头像
     * ip 更新IP
     * lat 地理纬度
     * lng 地理经度
     * adddate 添加时间
     */
    @Login
    @GetMapping("/get_online_user_list")
    @ApiOperation("获取在线用户列表")
    public R getOnlineUserList(){
        return R.ok(mixedOnlineUserService.selectList(new EntityWrapper<MixedOnlineUserEntity>()));
    }

    /**
     *获取榜单列表
     * @param liveUid 直播室编号 0 代表全站
     * @param category 类别 week=周榜,month=月榜,total=总榜 hour=小时榜（小时榜为全平台所有直播）
     * 数据库 [ 在线用户列表 ] tb_mixed_online_user
     * @return
     * uid 用户编号
     * category 类别 week=周榜,month=月榜,total=总榜 hour=小时榜（小时榜为全平台所有直播）
     * live_uid 直播室编号 0 代表全站
     * uid 用户编号
     * nickname 昵称
     * rank 排位
     * coins 金币数
     * adddate 更新时间
     *
     * 当前直播的的排位信息，只有全平台小时榜才有此值
     * "my":{
     *
     * }
     */
    @Login
    @GetMapping("/get_rewards_toplist")
    @ApiOperation("获取榜单列表")
    public R getRewardsToplist(@RequestParam(defaultValue = "0") Integer liveUid,String category){
        Long uid = (Long) HttpContextUtils.getHttpServletRequestHeaderValue("uid");
        if(StringUtils.isBlank(category)){
            return R.response(ResponseCode.FAIL,"缺少类型参数");
        }
        EntityWrapper wrapper=new EntityWrapper<MixedRewardsToplistEntity>();
        if(liveUid.intValue()>0&&!"hour".equals(category)){
            wrapper.eq("live_uid",liveUid);
        }
        List<MixedRewardsToplistEntity> mixedRewardsToplistEntityList=mixedRewardsToplistService.selectList(wrapper.eq("category",category)
                .orderBy("coins",false));
        //全平台小时榜
        if("hour".equals(category)){
            return R.ok(mixedRewardsToplistEntityList)
                    .put("my",mixedRewardsToplistService.selectOne(new EntityWrapper<MixedRewardsToplistEntity>()
                            .eq("live_uid",liveUid).eq("category",category)));
        }
        return R.ok(mixedRewardsToplistEntityList);
    }

    /**
     *定位匹配
     * @param lat 经度
     * @param lng 纬度
     * @param distance 距离
     * 数据库 [ 在线用户列表 ] tb_mixed_online_user
     * @return
     * "avanta": "http://app-dev-1301591806.cos.ap-shanghai.myqcloud.com/assets/user/defaultAvatar/108.png",//头像
     * "distance": "2122",//距离
     * "nickname": "太阳神",//昵称
     * "sense": "",//形象照
     * "uid": 7，//uid
     */
    @Login
    @GetMapping("/get_similar_location_user_list")
    @ApiOperation("定位匹配")
    public R getSimilarLocationUserList(String lat,String lng,@RequestParam(required = false) String distance){
        Long uid = (Long) HttpContextUtils.getHttpServletRequestHeaderValue("uid");
        if(StringUtils.isBlank(lat)){
            return R.response(ResponseCode.FAIL,"缺少经度参数");
        }
        if(StringUtils.isBlank(lng)){
            return R.response(ResponseCode.FAIL,"缺少纬度参数");
        }
        Map<String,Object> para=new HashMap<>();
        para.put("uid",uid);
        ViewUserOnlineListEntity viewUserOnlineListEntity=viewUserOnlineListService.selectById(uid);
        if(null==viewUserOnlineListEntity){
            return R.error(-101,"请重新登录");
        }
        int gender=viewUserOnlineListEntity.getGender();
        if(!"-1".equals(gender)){
            para.put("gender",gender==0?1:0);
        }

        para.put("lat",lat);
        para.put("lng",lng);
        if(StringUtils.isNotBlank(distance)){
            para.put("distance",distance);
        }
        return R.ok(viewUserOnlineListService.getSimilarLocationUserList(para));
    }

    /**
     *兴趣匹配
     * @param tag 兴趣标签
     * @param gender 性别 1 男 0 女 -1不限
     * 数据库 [ 在线用户列表 ] tb_mixed_online_user
     * @return
     *  "adddate": 1606458494,
     *  "gender": 0,
     *  "ip": "",
     *  "isOnline": 1,
     *  "lastUptime": 1606458494,
     *  "lat": "",
     *  "lng": "",
     *  "nickname": "绿城彭于晏",
     *  "realname": "",
     *  "tags": "篮球,国服无敌,无敌",
     *  "uid": 3，
     *  "isLiked":0  1=已喜欢 0=未喜欢
     */
    @Login
    @GetMapping("/get_similar_interest_user_list")
    @ApiOperation("兴趣匹配")
    public R getSimilarInterestUserList(String tag,@RequestParam(defaultValue = "0")String gender){
        Long uid = (Long) HttpContextUtils.getHttpServletRequestHeaderValue("uid");
        if(StringUtils.isBlank(gender)){
            return R.response(ResponseCode.FAIL,"缺少性别参数");
        }
        ViewUserOnlineListEntity viewUserOnlineListEntity=viewUserOnlineListService.selectById(uid);
        if(null==viewUserOnlineListEntity){
            return R.error(-101,"请重新登录");
        }
        Map<String,Object> para=new HashMap<>();
        para.put("lat",viewUserOnlineListEntity.getLat());
        para.put("lng",viewUserOnlineListEntity.getLng());
        para.put("uid",uid);
        para.put("page","1");
        para.put("limit","10");
        if(!StringUtils.isBlank(tag)){
            para.put("tags",new ArrayList<>(Arrays.asList(tag.split(","))));
        }
        if(!"-1".equals(gender)){
            para.put("gender",gender);
        }
        //para.put("is_online",1);
        return R.ok(viewUserOnlineListService.getSimilarInterestUserList(para));
    }

    /**
     *形象匹配
     * @param gender 性别 1 男 0 女 -1不限
     * 数据库 [ 在线用户列表 ] tb_mixed_online_user
     * @return
     *  "adddate": 1606458494,
     *  "gender": 0,
     *  "ip": "",
     *  "isOnline": 1,
     *  "lastUptime": 1606458494,
     *  "lat": "",
     *  "lng": "",
     *  "nickname": "绿城彭于晏",
     *  "realname": "",
     *  "tags": "篮球,国服无敌,无敌",
     *  "uid": 3,
     *  "isLiked":0  1=已喜欢 0=未喜欢
     */
    @Login
    @GetMapping("/get_similar_sence_user_list")
    @ApiOperation("形象匹配")
    public R getSimilarSenceUserList(@RequestParam(defaultValue = "0")String gender){
        Long uid = (Long) HttpContextUtils.getHttpServletRequestHeaderValue("uid");
        if(StringUtils.isBlank(gender)){
            return R.response(ResponseCode.FAIL,"缺少性别参数");
        }
        Map<String,Object> para=new HashMap<>();
        para.put("uid",uid);
        if(!"-1".equals(gender)){
            para.put("gender",gender);
        }
        return R.ok(viewUserOnlineListService.getSimilarSenceUserList(para));
    }

}
